package org.swfer.UICExtend 
{
	/**
	 * 时间节点对象
	 * @author swfer
	 */
	public class TimeNode implements ITimeNode
	{
		/** 时间节点管理器。 **/
		public static var timerManager:ITimerManager=null;
		
		private var _death:Boolean = false;
		private var _startTime:int;
		private var _delay:int;
		private var _interval:int;
		private var _currentCount:uint;
		private var _repeatCount:uint;
		private var _pause:Boolean = false;
		public var onTime:Function = null;
		public var onTimeComplete:Function = null;
		
		/**
		 * 构造函数
		 * @param	interval 时间节点定时间隔 ms
		 * @param	delay	 延迟时长 ms
		 * @param	repeatCount 重复次数（默认0 无限次）
		 */
		public function TimeNode(interval:Number,delay:Number=0,repeatCount:uint=0) 
		{
			if (timerManager == null) throw new Error('时间管理着对象未初始化，时间节点不可用');
			
			timerManager.pushNode(this);
			this.interval = interval;
			_repeatCount = int(repeatCount);
			this._delay = delay;
		}
		
		/**
		 * 销毁时间节点
		 */
		public function kill():void
		{
			_death = true;
			timerManager.removeNode(this);
		}
		
		private function exec():void
		{
			if (Boolean(onTime)) onTime(this);
			if (_currentCount == _repeatCount && repeatCount>0 && Boolean(onTimeComplete)) onTimeComplete(this); 
		}
		
		/**
		 * (inherit 禁止写入)当前定时次数
		 */
		public function get currentCount():uint 
		{
			return _currentCount;
		}
		public function set currentCount(value:uint):void 
		{
			_currentCount = value;
			exec();
		}
		
		/**
		 * (inherit 禁止写入)总共需要重复定时的次数
		 */
		public function get repeatCount():uint 
		{
			return _repeatCount;
		}
		public function set repeatCount(value:uint):void 
		{
			_repeatCount = value;
		}
		
		/**
		 * (inherit 禁止写入)定时间隔
		 */
		public function get interval():int 
		{
			return _interval;
		}
		public function set interval(value:int):void 
		{
			_interval = value;
		}
		
		/**
		 * (inherit 禁止写入)定时节点开始运行时刻
		 */
		public function get startTime():int 
		{
			return _startTime;
		}
		public function set startTime(value:int):void 
		{
			_startTime = value;
		}
		
		/**
		 * (inherit 禁止写入)延迟时间
		 */
		public function get delay():int 
		{
			return _delay;
		}
		public function set delay(value:int):void 
		{
			_delay = value;
		}
		
		/**
		 * 定时间隔是否死亡
		 */
		public function get death():Boolean 
		{
			return _death;
		}
		public function set death(value:Boolean):void 
		{
			_death = value;
		}
		
		/**
		 * 是否暂停节点
		 */
		public function get pause():Boolean 
		{
			return _pause;
		}
		public function set pause(value:Boolean):void 
		{
			_pause = value;
		}
		
	}

}